package sistema;

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {

  public synchronized static void sendMail(String assunto, String mensagem, String destinatario) {
    //Properties props = System.getProperties(); *** mesmo caso. ***
    Properties props = new Properties();

    props.put("mail.smtp.host", "gmail-smtp.l.google.com");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

    try {
      Session session = Session.getDefaultInstance(props, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication("multitad@gmail.com", "corp160tad");
        }
      });
      
      MimeMessage message = new MimeMessage(session);
      message.setText(mensagem);
      message.setSubject(assunto);
      message.setFrom(new InternetAddress("multitad@gmail.com"));
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(destinatario));
      Transport.send(message);
    } catch (MessagingException mex) {
      mex.printStackTrace();
    }
  }
}